package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.ProRightsEntity;
import com.dragon.modules.yuegou.enums.code.ProductCode;
import com.dragon.modules.yuegou.mapper.member.ProRightsMapper;
import com.dragon.modules.yuegou.model.member.ProRights;
import com.dragon.modules.yuegou.query.member.ProRightsQuery;
import com.dragon.modules.yuegou.request.SaveProRightsByGoodsRequest;
import com.dragon.modules.yuegou.request.SaveProRightsByRightsRequest;
import com.dragon.modules.yuegou.service.member.ProRightsService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品权益关联表 服务实现
 *
 * @author pbuilder generated
 * @date 2021-12-15 09:38:01
 * @version 2.0
 */
@Service("memProRightsService")
public class ProRightsServiceImpl
		extends ServiceImpl<ProRightsMapper, ProRightsEntity>
		implements ProRightsService {


	@Override
	@Transactional
	public void saveProRightsByRights(SaveProRightsByRightsRequest request) {
		Check.notNull(request,"商品及权益信息不能为空");
		Check.notEmpty(request.getRightsId(),"权益ID不能为空");
		Check.notEmpty(request.getGoods(),"商品信息不能为空");
		ProRightsQuery query = new ProRightsQuery();
		query.setRightsId(request.getRightsId());
		remove(query.toQueryWrapper());
		request.getGoods().forEach(item -> {
			ProRights proRights = new ProRights();
			proRights.setGoodsId(item.getGoodsId());
			proRights.setGoodsName(item.getGoodsName());
			proRights.setRightsId(request.getRightsId());
			proRights.setRightsName(request.getRightsName());
			save(proRights);
		});
	}

	@Override
	@Transactional
	public void saveProRightsByGoods(SaveProRightsByGoodsRequest request) {
		Check.notNull(request,"商品及权益信息不能为空");
		Check.notEmpty(request.getGoodsId(), ProductCode.SKUID_NOTNULL.getMessage());
		Check.notEmpty(request.getRights(),"权益信息不能为空");
		ProRightsQuery query = new ProRightsQuery();
		query.setGoodsId(request.getGoodsId());
		remove(query.toQueryWrapper());
		request.getRights().forEach(item -> {
			ProRights proRights = new ProRights();
			proRights.setGoodsId(request.getGoodsId());
			proRights.setGoodsName(request.getGoodsName());
			proRights.setRightsId(item.getRightsId());
			proRights.setRightsName(item.getRightsName());
			 save(proRights);
		});
	}
}